package com.sand.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sand.Exception.SandException;
import com.sand.dto.worker.AddProjectWorker;
import com.sand.dto.worker.DownProjectWorkers;
import com.sand.entity.work.Salary;
import com.sand.entity.work.Project_Worker;
import com.sand.mapper.Project_workerMapper;
import com.sand.result.AppHttpCodeEnum;
import com.sand.service.Project_workerService;
import com.sand.service.SalaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class Project_workerServiceImpl extends ServiceImpl<Project_workerMapper, Project_Worker> implements Project_workerService {

    @Autowired
    private SalaryService salaryService;


    /**
     * 安排工人干活
     *
     * @param list
     */
    @Override
    @Transactional
    public Integer add(AddProjectWorker[] list) {

        //存放要添加到工程的工人
        ArrayList<Project_Worker> projectWorkers = new ArrayList<>();

        //存放要添加到工资表的数据
        ArrayList<Salary> salaryArrayList = new ArrayList<>();

        //遍历数据
        for (AddProjectWorker addProjectWorker : list) {

            Integer merchantid = addProjectWorker.getMerchantid();
            Integer projectid = addProjectWorker.getProjectid();
            Integer workerid = addProjectWorker.getWorkerid();
            Integer ysalary = addProjectWorker.getYsalary();

            //添加到工程
            Project_Worker projectWorker = new Project_Worker();

            projectWorker.setProjectid(projectid);
            projectWorker.setWorkerid(workerid);

            //添加
            projectWorkers.add(projectWorker);

            //添加工资
            Salary salary = new Salary();

            salary.setProjectid(projectid);
            salary.setMerchantid(merchantid);
            salary.setWorkerid(workerid);
            salary.setYsalary(ysalary);
            salary.setSsalary(0); //默认不发工资
            salary.setWhether(0); //默认没有付清工资

            //添加
            salaryArrayList.add(salary);

        }

        //添加工人工程
        saveBatch(projectWorkers);

        //添加工人工资
        salaryService.saveBatch(salaryArrayList);


        return 1;
    }

    /**
     * 解雇工人
     *
     * @param downProjectWorkers
     */
    @Override
    public Integer down(DownProjectWorkers downProjectWorkers) {

        Integer projectid = downProjectWorkers.getProjectid();
        Integer workerid = downProjectWorkers.getWorkerid();

        if (projectid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入工程id");
        }

        if (workerid == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"没有传入工人id");
        }

        //判断工人是否存在与工程中
        Project_Worker one = getOne(new LambdaQueryWrapper<Project_Worker>().eq(Project_Worker::getProjectid, projectid).eq(Project_Worker::getWorkerid, workerid));

        if (one == null){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"该工人不在这个工程中");
        }

        //判断这个工人工资是否付清
        Salary salary = salaryService.getOne(new LambdaQueryWrapper<Salary>().eq(Salary::getProjectid, projectid).eq(Salary::getWorkerid, workerid));

        if (salary.getWhether() == 0){
            throw new SandException(AppHttpCodeEnum.PARAM_INVALID,"当前工人工资未清算无法删除该工人");
        }

        //删除
        removeById(one.getId());

        return 1;
    }


}
